package com.example.mychat.Controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mychat.entity.Authorities;
import com.example.mychat.entity.Friend;
import com.example.mychat.entity.Request;
import com.example.mychat.mapper.AuthoritiesMapper;
import com.example.mychat.mapper.FriendMapper;
import com.example.mychat.mapper.RequestMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Tag(name = "好友管理")
@Controller
@RequestMapping("/friend")
public class FriendController {
    @Resource
    FriendMapper friendMapper;

    @Resource
    AuthoritiesMapper authoritiesMapper;

    @Resource
    RequestMapper requestMapper;

    @Resource
    HttpServletRequest req;

    public int getUserId() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        Authorities authority = authoritiesMapper.selectOne(queryWrapper);
        int userId = authority.getId();
        return userId;
    }

    @Operation(description = "申请添加好友")
    @PostMapping("/request")
    @ResponseBody
    public String request(@RequestParam int requestId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return "用户未登录";
        }
        int userId = getUserId();
        if (userId == requestId) {
            return "无法给自己发送好友申请";
        }
        requestMapper.insert(new Request(userId, requestId));
        return "success";
    }

    @Operation(description = "查看好友申请")
    @GetMapping("/request")
    @ResponseBody
    public List<Request> getRequestList() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        int userId = getUserId();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("requestId", userId);
        List<Request> requestList = requestMapper.selectList(queryWrapper);
        return requestList;
    }

    @Operation(description = "通过好友申请")
    @PostMapping("/accept")
    @ResponseBody
    public String acceptRequest(@RequestParam int requestId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return "用户未登录";
        }
        int userId = getUserId();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", requestId);
        queryWrapper.eq("requestId", userId);
        List<Request> requestList = requestMapper.selectList(queryWrapper);
        if (requestList.size() == 0) {
            return "不存在该好友申请，通过失败";
        }
        requestMapper.delete(queryWrapper);
        friendMapper.insert(new Friend(userId, requestId));
        friendMapper.insert(new Friend(requestId, userId));
        return "success";
    }

    @Operation(description = "查看好友列表")
    @GetMapping("/getFriend")
    @ResponseBody
    public List<Friend> getFriendList() {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return null;
        }
        int userId = getUserId();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        List<Friend> friendList = friendMapper.selectList(queryWrapper);
        return friendList;
    }

    @Operation(description = "删除好友")
    @DeleteMapping("/delete/{friendId}")
    @ResponseBody
    public String deleteFriend(@PathVariable int friendId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return "用户未登录";
        }
        int userId = getUserId();
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userId", userId);
        queryWrapper1.eq("friendId", friendId);
        List<Friend> friendList = friendMapper.selectList(queryWrapper1);
        if (friendList.size() == 0) {
            return "好友不存在，删除失败";
        }
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("userId", friendId);
        queryWrapper2.eq("friendId", userId);
        friendMapper.delete(queryWrapper1);
        friendMapper.delete(queryWrapper2);
        return "success";
    }

    @Operation(description = "好友分组")
    @PostMapping("/group")
    @ResponseBody
    public String groupFriend(@RequestParam int groupId, @RequestParam int friendId) {
        String username = String.valueOf(req.getSession().getAttribute("username"));
        if (username.equals("null")) {
            return "用户未登录";
        }
        int userId = getUserId();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("friendId", friendId);
        List<Friend> friendList = friendMapper.selectList(queryWrapper);
        if (friendList.size() == 0) {
            return "好友不存在，无法进行分组";
        }
        friendMapper.insert(new Friend(userId, friendId, groupId));
        return "success";
    }
}
